package deployr.domain;

import static deployr.core.Streams.*;
import static deployr.core.Streams.*;
import static deployr.core.Checksums.*;
import java.io.*;

public class Deliverable implements Serializable, IDeliverable {

	private static final long serialVersionUID = -6028368582720252832L;

	private final String md5;

	private final String name;
	
	private final byte[] bytes;

	private final String updateKey;

	public String getUpdateKey() {
		return updateKey;
	}
	
	public Deliverable(String updateKey, String name, byte[] bytes) {
		this.updateKey = updateKey;
		this.name = name;
		this.md5 = sha1(bytes);
		this.bytes = bytes;
	}

	@Override
	public boolean equals(final Object obj) {
		if (!Deliverable.class.isAssignableFrom(obj.getClass())) return false;
		if (!((Deliverable) obj).md5.equals(md5)) return false;
		if (!((Deliverable) obj).name.equals(name)) return false;
		return true;
	}

	public byte[] getContent() {
		return bytes;
	}

	@Override
	public String getMd5() {
		return md5;
	}

	public String getName() {
		return name;
	}

	public void writeTo(OutputStream outputStream) {
		write(bytes, outputStream);
	}
}
